home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / gtlayout-source.lha / LTP_GlyphSetup.c < prev    next >
C/C++ Source or Header  |  1996-10-07  |  3KB  |  178 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1996 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. VOID
  15. LTP_GetDefaultFont(struct TTextAttr *TextAttr)
  16. {
  17.     struct RastPort LocalRastPort;
  18.  
  19.     InitRastPort(&LocalRastPort);
  20.  
  21.     memset(TextAttr,0,sizeof(*TextAttr));
  22.  
  23.     AskFont(&LocalRastPort,TextAttr);
  24. }
  25.  
  26.  
  27. /*****************************************************************************/
  28.  
  29.  
  30. struct TextFont *
  31. LTP_OpenFont(struct TextAttr *TextAttr)
  32. {
  33.     struct TextFont *Font;
  34.  
  35.         // Let's see if this one is special
  36.  
  37.     if(TextAttr == (struct TextAttr *)~0)
  38.     {
  39.         struct TTextAttr LocalTextAttr;
  40.  
  41.         memset(&LocalTextAttr,0,sizeof(LocalTextAttr));
  42.  
  43.         Forbid();
  44.  
  45.             // Borrow the system default font data
  46.  
  47.         LTP_GetDefaultFont(&LocalTextAttr);
  48.  
  49.             // Let's hope the font will open...
  50.  
  51.         Font = OpenFont((struct TextAttr *)&LocalTextAttr);
  52.  
  53.         Permit();
  54.     }
  55.     else
  56.     {
  57.         if(!(Font = OpenFont(TextAttr)))
  58.         {
  59.             if(FindTask(NULL)->tc_Node.ln_Type != NT_TASK)
  60.             {
  61.                 struct Library *DiskfontBase;
  62.  
  63.                 if(DiskfontBase = OpenLibrary("diskfont.library",0))
  64.                 {
  65.                     Font = OpenDiskFont(TextAttr);
  66.  
  67.                     CloseLibrary(DiskfontBase);
  68.                 }
  69.             }
  70.         }
  71.     }
  72.  
  73.     return(Font);
  74. }
  75.  
  76.  
  77. /*****************************************************************************/
  78.  
  79.  
  80. BOOL
  81. LTP_GlyphSetup(struct LayoutHandle *Handle,struct TextAttr *TextAttr)
  82. {
  83.     struct TextFont *Font;
  84.  
  85.     if(!TextAttr)
  86.         TextAttr = Handle->Screen->Font;
  87.  
  88.     if(Font = LTP_OpenFont(Handle->TextAttr = TextAttr))
  89.     {
  90.         struct RastPort *RPort;
  91.         LONG Count;
  92.         LONG Total;
  93.         ULONG OldStyle;
  94.         LONG GlyphWidth;
  95.         LONG i;
  96.         UBYTE Glyph;
  97.  
  98.         RPort = &Handle->RPort;
  99.  
  100.         if(Handle->CloseFont)
  101.             CloseFont(RPort->Font);
  102.  
  103.         Handle->CloseFont = TRUE;
  104.  
  105.         LTP_SetFont(Handle,Font);
  106.  
  107.         OldStyle = RPort->AlgoStyle;
  108.  
  109.         SetSoftStyle(RPort,FSF_BOLD | FSF_UNDERLINED,FSF_BOLD | FSF_UNDERLINED);
  110.  
  111.             // Cover all printable characters
  112.  
  113.         Count = Total = 0;
  114.  
  115.         for(i = 32 ; i < 256 ; i++)
  116.         {
  117.             if(i == 128)
  118.                 i = 160;
  119.  
  120.             Glyph = i;
  121.  
  122.             GlyphWidth = TextLength(RPort,&Glyph,1);
  123.  
  124.                 // For really pathologic fonts...
  125.  
  126.             if(GlyphWidth >= 0 && GlyphWidth <= 32767)
  127.             {
  128.                 Total += GlyphWidth;
  129.  
  130.                 Count++;
  131.             }
  132.         }
  133.  
  134.         Handle->GlyphWidth = Total / Count;
  135.  
  136.             // Now for the numeric characters
  137.  
  138.         for(Total = 0, i = '0' ; i <= '9' ; i++)
  139.         {
  140.             Glyph = i;
  141.  
  142.             Total += TextLength(RPort,&Glyph,1);
  143.         }
  144.  
  145.         Total /= 10;
  146.  
  147.             // Actually, the numeric character should be
  148.             // just as wide as the space character. Let's
  149.             // hope for the best.
  150.  
  151.         Count = TextLength(RPort," ",1);
  152.  
  153.         if(Total < Count)
  154.             Total = Count;
  155.  
  156.         if(Total > Handle->GlyphWidth)
  157.             Handle->GlyphWidth = Total;
  158.  
  159.         if(Handle->GlyphWidth <= 8)
  160.             Handle->InterWidth = 4;
  161.         else
  162.             Handle->InterWidth = Handle->GlyphWidth / 2;
  163.  
  164.         Handle->GlyphHeight = RPort->TxHeight;
  165.  
  166.         if(Handle->GlyphHeight <= 8)
  167.             Handle->InterHeight = 2;
  168.         else
  169.             Handle->InterHeight = Handle->GlyphHeight / 4;
  170.  
  171.         SetSoftStyle(RPort,OldStyle,FSF_BOLD | FSF_UNDERLINED);
  172.  
  173.         return(TRUE);
  174.     }
  175.     else
  176.         return(FALSE);
  177. }
  178.